package cn.jdy.audit.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.audit.entity.AdShow;
import cn.jdy.audit.entity.SeRecord;
import cn.jdy.audit.entity.UrlRecord;
import cn.jdy.audit.service.AdShowService;
import cn.jdy.audit.service.SeRecordService;
import cn.jdy.audit.service.UrlRecordService;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.framework.util.SpringUploadFile;
import cn.jdy.platform.service.ConfService;

/**
 * @title 大数据搜索后台操作
 * @author jga
 * @since 1-11-2016
 */
@Controller
@RequestMapping("/adShow")
public class AdShowCtrl extends BaseCtrl{
	@Autowired
	private AdShowService adShowService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/adShow/list?s=";
	
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(AdShow entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/audit/adShow/adShowList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(AdShow adshow, String fromDate, String toDate){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", adShowService.query(adshow, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", adShowService.count(adshow, fromDate, toDate));
			jsonMap.put("iTotalRecords", adShowService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView("/audit/adShow/adShowDetail.jsp");
		System.out.println(this.confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L).size());
		//mv.addObject("column", this.confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("data", id==null||id==0 ? id : this.adShowService.find(id));
		return mv;
	}
	
	/**
	 * 保存信息
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(AdShow adshow, String file) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		if(adshow.getId()==null) {//
			adshow.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.adShowService.save(adshow);
				mv.setViewName(this.viewName.concat("1"));
			} else {//更新
				this.adShowService.update(adshow);
				mv.setViewName(this.viewName.concat("2"));
			}
    jsonMap.put("statusCode", 200);
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return mv;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.adShowService.delete(id);
			 mv.setViewName(this.viewName.concat("3"));
	      // 清理内容页缓存
	      NginxPurgeCacheUtil.purge(4, id);
		} catch(Exception e) {
	     mv.setViewName(this.viewName.concat("4"));
	      BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}
}
